package com.dq.health.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dto.request.common.BaseIdRequest;
import com.dq.health.dto.request.outpatient.QueryOutpatientParam;
import com.dq.health.dto.response.outpatient.OutpatientOrderInfo;
import com.dq.health.enums.OutpatientOrderStatusEnum;
import com.dq.health.service.IAPIOutpatientService;
import com.dq.health.utils.SecurityUtils;
import com.dq.health.utils.data.JsonUtil;
import com.dq.health.utils.view.SimpleView;
import com.dq.health.utils.view.StringView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <门诊预约单>
 *
 * @Author: chenxin
 * @Date: 2021/5/12
 */
@RestController
@RequestMapping("/outpatient")
@Api(tags = "门诊预约单控制器")
public class OutpatientController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OutpatientController.class);

    @Autowired
    private IAPIOutpatientService outpatientService;

    @PostMapping("/queryMyOutpatientList")
    @ApiOperation("查询我的门诊预约单列表")
    public SimpleView<IPage<OutpatientOrderInfo>> queryMyOutpatientList(@RequestBody QueryOutpatientParam request) {
        LOGGER.info("### queryMyOutpatientList req:{}", JsonUtil.toString(request));
        SimpleView<IPage<OutpatientOrderInfo>> view = new SimpleView<>();

        Long loginUserId = SecurityUtils.getLoginUserId();
        request.setDoctorUserId(loginUserId);
        IPage<OutpatientOrderInfo> outpatientOrderInfoIPage = outpatientService.queryList(request);
        view.success(outpatientOrderInfoIPage);

        return view;
    }

    @PostMapping("/queryOrderDetail")
    @ApiOperation("查询预约单详情")
    public SimpleView<OutpatientOrderInfo> queryOrderDetail(@RequestBody BaseIdRequest request) {
        LOGGER.info("### queryOrderDetail req:{}", JsonUtil.toString(request));
        SimpleView<OutpatientOrderInfo> view = new SimpleView<>();

        view.success(outpatientService.queryInfoById(request.getId()));

        return view;
    }

    @PostMapping("/doneOutpatientOrder")
    @ApiOperation("完成预约单")
    public StringView doneOutpatientOrder(@RequestBody BaseIdRequest request) {
        LOGGER.info("### 完成预约单 req:{}", JsonUtil.toString(request));

        outpatientService.updateOrderStatus(request.getId(), SecurityUtils.getLoginUserId(),
            OutpatientOrderStatusEnum.END.getValue());

        StringView view = new StringView();
        view.success();
        return view;
    }

}
